<?php


namespace app\api\controller\v1;


use app\api\service\Lua;
use app\common\lib\redEnvelopes\LuckyRedEnvelope;
use app\common\lib\sms\TencentSms;
use app\common\menu\Common;
use app\common\service\Redis;
use app\common\exception\NotFoundException;
use app\common\exception\ParameterFailedException;
use app\common\exception\RuntimeException;
use app\common\lib\sms\AliSms;
//use app\common\lib\rabbitMQ\RabbitMQ;
use app\common\service\Zookeeper;
use Exception;
use think\Db;
use think\facade\Log;
use think\response\Json;

class Test extends V1BaseController
{
    public function receive()
    {
        try {

            $zk = new Zookeeper('192.168.3.150:2181');

            $zk->makeNode('/test123', 'abc', [
                [
                    'perms'  => \Zookeeper::PERM_ALL,
                    'scheme' => 'world',
                    'id'     => 'anyone',
                ]
            ]);
            //$zk->set('/test123', 'abc');

            var_dump($zk->deleteNode('/foo'));
            //var_dump($zk->deleteNode('/temp'));
            //var_dump($zk->deleteNode('/bar'));
            //var_dump($zk->deleteNode('/test123'));
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }
        //die;
        //var_dump($zk->get('/'));
        //var_dump($zk->getChildren('/'));
        //var_dump($zk->get('/test123'));
        //var_dump($zk->getChildren('/'));
        //
        //var_dump($zk->set('/foo/001', 'bar1'));
        //var_dump($zk->set('/foo/002', 'bar2'));
        //var_dump($zk->getChildren('/foo'));

        //watch example
        function callback()
        {
            echo "in watch callback\n";
        }

        $zk->set('/bar', 1);
        $ret = $zk->watch('/bar', 'callback');
        $zk->set('/bar', 2);
        while (true) {
            sleep(1);
            zookeeper_dispatch();
        }

        //$zookeeper->set('/temp/liguoxin', 'value');
        die;
        var_dump(2);
        die;
        //RabbitMQ::receive();
        //$templateParam = [
        //    'name' => '李国欣',
        //    'time' => date('Y-m-d H:i:s')
        //];
        //TencentSms::send('15622535674', '600031', $templateParam);
    }

    /**
     * @return Json
     * @throws Exception
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/4/27
     * @Time: 21:51
     */
    public function test()
    {
        echo phpinfo();die;
        $id = \Ramsey\Uuid\Uuid::uuid1();
        var_dump($id);die;

        Redis::getRedis();
        var_dump(1);
        die;
        //RabbitMQ::receive();
        //RabbitMQ::send();
        //$templateParam = [
        //    'name' => '李国欣',
        //    'time' => date('Y-m-d H:i:s')
        //];
        //AliSms::send('15622535674', Common::SMS_ALI_TEMPLATE_CHANGE_PASSWORD, $templateParam);
        die;
        //echo phpinfo();die;
        //        $redis = Redis::getRedis();
        //        $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_JSON);
        //
        //         $redis->set('key', ['asd' => 'as', 'dd' => 123, 'b' => true]);
        //         var_dump($redis->get('key'));
        //
        //        die;
        //        //$redis->lInsert();
        //        $amount = $redis->lpop('yiqi_welfare_' . 1);
        //
        //        var_dump($amount);
        //die;

        //(new LuckyRedEnvelope())->generate(100,'10','10000');
        //die;
        (new LuckyRedEnvelope())->rob(17, '1000');
        die;

        Db::startTrans();
        try {
            (new LuckyRedEnvelope())->rob(1, '10000');

            Db::commit();
            return json(['msg' => '您抢到了红包']);
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }

        //悲观锁
        //    实现方式：mysql追加 for update
        //    缺点：mysql压力剧增，并且阻塞进程造成性能下降，原因是乐观锁是抢锁的意思，没抢到锁的请求则等待或终止
        //乐观锁
        //    实现方式：ACS原理，version
        //    缺点：ASC有ABA问题，ABA是值在判断A时，另一个请求单位时间内将version改成b，再改成a，流程完成后第一个请求还是判定为成功
        //    如下图
        //        解决方案：重入机制，时间重入、次数重入(优选)
        //乐观锁的方案对mysql要求过高，容易引起mysql宕机
        //    解决方案：使用redis内存操作，速度快，lua脚本保证原子性
    }
}
